﻿using System.Windows;
using System.Windows.Controls;
using ImageAppViewModels;

namespace FindDuplicates.Plugin
{
    public partial class CompareImagesView : UserControl
    {
        public static readonly DependencyProperty FileImageViewModel1Property =
            DependencyProperty.Register("FileImageViewModel1", typeof(FileImageViewModel), typeof(CompareImagesView), new PropertyMetadata(null, OnFileImageViewModel1PropertyChanged));

        public static readonly DependencyProperty FileImageViewModel2Property =
            DependencyProperty.Register("FileImageViewModel2", typeof(FileImageViewModel), typeof(CompareImagesView), new PropertyMetadata(null, OnFileImageViewModel2PropertyChanged));

        public FileImageViewModel FileImageViewModel1
        {
            get { return (FileImageViewModel)GetValue(FileImageViewModel1Property); }
            set { SetValue(FileImageViewModel1Property, value); }
        }

        public FileImageViewModel FileImageViewModel2
        {
            get { return (FileImageViewModel)GetValue(FileImageViewModel2Property); }
            set { SetValue(FileImageViewModel2Property, value); }
        }

        private static void OnFileImageViewModel1PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CompareImagesView source = d as CompareImagesView;
            FileImageViewModel v = e.NewValue as FileImageViewModel;
            FileImageViewModel vOld = e.OldValue as FileImageViewModel;

            if (v != vOld)
            {
                source.UpdateFileImageViewModel1(v, vOld);
            }            
        }

        private static void OnFileImageViewModel2PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CompareImagesView source = d as CompareImagesView;
            FileImageViewModel v = e.NewValue as FileImageViewModel;
            FileImageViewModel vOld = e.OldValue as FileImageViewModel;

            if (v != vOld)
            {
                source.UpdateFileImageViewModel2(v, vOld);
            }
        }

        private void UpdateFileImageViewModel1(FileImageViewModel viewModel, FileImageViewModel old)
        {
            FileImageViewModel1 = viewModel;
            DoBinding1();
        }

        public void DoBinding1()
        {
            FileImageViewModel1.LoadBitmap();
            imgMain1.FileImageViewModel = FileImageViewModel1;
            imgMain1.InvalidateVisual();
            gridForFile1.DataContext = FileImageViewModel1;
            InvalidateVisual();
        }

        public void DoBinding2()
        {
            FileImageViewModel2.LoadBitmap();
            imgMain2.FileImageViewModel = FileImageViewModel2;
            imgMain2.InvalidateVisual();
            gridForFile2.DataContext = FileImageViewModel2;
            InvalidateVisual();
        }

        private void UpdateFileImageViewModel2(FileImageViewModel viewModel, FileImageViewModel old)
        {
            FileImageViewModel2 = viewModel;
            DoBinding2();
        }

        public CompareImagesView()
        {
            InitializeComponent();
        }        
    }
}
